package com.learn.micro_mall.service.api.brand.impl;

import com.learn.micro_mall.common.response.PlainResult;
import com.learn.micro_mall.common.util.PlainResultUtils;
import com.learn.micro_mall.domain.service.brand.BrandValidatorDomainService;
import com.learn.micro_mall.domain.service.brand.InvalidBrandDomainService;
import com.learn.micro_mall.domain.entity.brand.BrandEntity;
import com.learn.micro_mall.repository.brand.BrandRepository;
import com.learn.micro_mall.service.api.brand.BrandService;
import com.learn.micro_mall.service.convertor.brand.BrandMapper;
import com.learn.micro_mall.service.request.brand.BrandDeleteRequest;
import com.learn.micro_mall.service.request.brand.BrandCreateRequest;
import com.learn.micro_mall.service.request.brand.BrandUpdateRequest;
import com.learn.micro_mall.service.response.brand.BrandDeleteResponse;
import com.learn.micro_mall.service.response.brand.BrandCreateResponse;
import com.learn.micro_mall.service.response.brand.BrandUpdateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/brand")
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandValidatorDomainService brandValidatorDomainService;

    @Autowired
    private InvalidBrandDomainService invalidBrandDomainService;

    @Resource
    private BrandRepository brandRepository;

    @Override
    @RequestMapping("/create")
    public PlainResult<BrandCreateResponse> createBrand(BrandCreateRequest request) throws Exception {
        BrandEntity brandEntity = BrandMapper.INSTANCES.toBrandEntity(request);
        brandEntity.valid();
        brandValidatorDomainService.validatorCreateBrandParam(brandEntity);
        brandRepository.insertBrandEntity(brandEntity);
        BrandCreateResponse response = new BrandCreateResponse();
        response.setSuccess(true);
        return PlainResultUtils.success(response);

    }

    @Override
    @RequestMapping("/update")
    public PlainResult<BrandUpdateResponse> updateBrand(BrandUpdateRequest request) throws Exception {
        BrandEntity brandEntity = BrandMapper.INSTANCES.toBrandEntity(request);
        brandEntity.valid();
        brandValidatorDomainService.validatorBrandHasExist(brandEntity.getBrandId().getBrandName());
        brandRepository.updateBrandEntity(brandEntity);
        BrandUpdateResponse response = new BrandUpdateResponse();
        response.setSuccess(true);
        return PlainResultUtils.success(response);
    }

    @Override
    @RequestMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public PlainResult<BrandDeleteResponse> deleteBrand(BrandDeleteRequest request) throws Exception {
        brandValidatorDomainService.validatorBrandHasExist(request.getBrandName());
        invalidBrandDomainService.invalidBrandAndGoodsRelation(request.getBrandName());
        BrandEntity brandEntity = BrandMapper.INSTANCES.toBrandEntity(request);
        brandRepository.deleteBrandEntity(brandEntity);
        BrandDeleteResponse response = new BrandDeleteResponse();
        response.setSuccess(true);
        return PlainResultUtils.success(response);
    }


}
